package com.huabo.system.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.JedisUtil;
import com.hbfk.util.JsonBean;
import com.hbfk.util.PageInfo;
import com.hbfk.util.ResponseFormat;
import com.huabo.system.config.DateBaseConfig;
import com.huabo.system.mysql.entity.TblManageRightMySql;
import com.huabo.system.mysql.entity.TblManageUserRightMySql;
import com.huabo.system.mysql.entity.TblOrganizationMySql;
import com.huabo.system.mysql.mapper.TblManageRightMySqlDAO;
import com.huabo.system.mysql.mapper.TblManageUserRightMySqlMapper;
import com.huabo.system.oracle.entity.TblBiReportMenu;
import com.huabo.system.oracle.entity.TblManageRight;
import com.huabo.system.oracle.entity.TblManageUserRight;
import com.huabo.system.oracle.entity.TblOrganization;
import com.huabo.system.oracle.mapper.TblManageRightDAO;
import com.huabo.system.oracle.mapper.TblManageUserRightMapper;
import com.huabo.system.service.TblManageRightService;
import com.huabo.system.utils.RedisFinalUtis;
import com.huabo.system.utils.Tree;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.beans.Transient;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;

@Service("TblManageRightService")
public class TblManageRightServiceImpl implements TblManageRightService {
    private static final Logger logger = LoggerFactory.getLogger(TblManageRightServiceImpl.class);

    @Resource
    private TblManageRightDAO tblManageRightDAO;

    @Resource
    private TblManageUserRightMapper tblManageUserRightMapper;

    @Resource
    private TblManageRightMySqlDAO tblManageRightMySqlDAO;

    @Resource
    private TblManageUserRightMySqlMapper tblManageUserRightMySqlMapper;

    @Override
    public TblManageRight findById(String id) {
        return this.tblManageRightDAO.findById(new BigDecimal(id));
    }

    @Override
    public TblManageRightMySql findByMySqlId(String id) {
        return this.tblManageRightMySqlDAO.findById(new BigDecimal(id));
    }

    @Override
    public List<Tree> getOrgFatherRightforRedis(TblOrganization org) throws Exception {
        if (null != org) {
            List<Tree> list = new ArrayList();
            List<TblManageRight> root = this.tblManageRightDAO.getTreebyorgidRoots(org.getOrgid().toString());

            Tree tree = new Tree();
            for (Iterator var4 = root.iterator(); var4.hasNext(); list.add(tree)) {
                TblManageRight tblManageRight = (TblManageRight) var4.next();
                //tree = new Tree();
                tree.setName(tblManageRight.getRightname());
                tree.setId(tblManageRight.getRightid());
                tree.setTarget("mainFramex");
                tree.setChecked(tblManageRight.getRightid() != null);
                tree.setpId(tblManageRight.getFatherrightid());
                tree.setUrl("");
                tree.setOpen(true);
                List<Tree> childre = this.addChildren(org.getOrgid(), tblManageRight.getRightid(), (List) root);
                if (childre.size() > 0) {
                    tree.setChildren(childre);
                }
            }

            return list;
        } else {
            return null;
        }
    }

    @Override
    public List<Tree> getMySqlOrgFatherRightforRedis(TblOrganizationMySql org) throws Exception {
        if (null != org) {
            List<Tree> list = new ArrayList();
            List<TblManageRightMySql> root = this.tblManageRightMySqlDAO.getTreebyorgidRoots(org.getOrgid().toString());

            Tree tree = new Tree();
            for (Iterator var4 = root.iterator(); var4.hasNext(); list.add(tree)) {
                TblManageRightMySql tblManageRight = (TblManageRightMySql) var4.next();
                //tree = new Tree();
                tree.setName(tblManageRight.getRightname());
                tree.setId(tblManageRight.getRightid());
                tree.setTarget("mainFramex");
                tree.setChecked(tblManageRight.getRightid() != null);
                tree.setpId(tblManageRight.getFatherrightid());
                tree.setUrl("");
                tree.setOpen(true);
                List<Tree> childre = this.addChildren(org.getOrgid(), tblManageRight.getRightid(), (List) root);
                if (childre.size() > 0) {
                    tree.setChildren(childre);
                }
            }

            return list;
        } else {
            return null;
        }
    }

    @Override
    public void findRightListBiDistribution(PageInfo<TblManageRight> pageInfo, BigDecimal orgid) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        pageInfo.setTlist(tblManageRightDAO.selectListSql(pageInfo, orgid));
        pageInfo.setTotalRecord(tblManageRightDAO.countRightid(pageInfo, orgid));
        resultMap.put("code", "1");
        resultMap.put("msg", "访问接口成功");
        resultMap.put("data", pageInfo);
    }

    @Override
    public void findMySqlRightListBiDistribution(PageInfo<TblManageRightMySql> mySqlPageInfo, BigDecimal orgid) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        mySqlPageInfo.setTlist(tblManageRightMySqlDAO.selectListSql(mySqlPageInfo, orgid));
        mySqlPageInfo.setTotalRecord(tblManageRightMySqlDAO.countRightid(mySqlPageInfo, orgid));
        resultMap.put("code", "1");
        resultMap.put("msg", "访问接口成功");
        resultMap.put("data", mySqlPageInfo);
    }

//    @Override
//    public void distributionPageIdByRightId(String[] rightids, String[] pageids, BigDecimal orgid) {
//        String sql = "";
//        Integer count = 0;
//        String[] var6 = rightids;
//        int var7 = rightids.length;
//
//        for(int var8 = 0; var8 < var7; ++var8) {
//            String rightid = var6[var8];
//            String[] var10 = pageids;
//            int var11 = pageids.length;
//            TblOrganization organization = new TblOrganization();
//            for(int var12 = 0; var12 < var11; ++var12) {
//                String pageid = var10[var12];
//               // sql = tblManageRightDAO.selectSql();
//                count = this.tblManageRightDAO.selectRightCoun(orgid,rightid);
//                if (count > 0) {
//                    tblManageRightDAO.deletePageId(pageid);
//                    this.tblManageRightDAO.executeSql(organization.getOrgid(),rightid,pageid,pageid);
//                }
//
//                tblManageRightDAO.insertOrgidPAGEIDMODULERIGHTID();
//                this.tblManageRightDAO.executeSql(organization.getOrgid(),rightid,pageid,pageid);
//                List<Object> childrenList = this.tblManageRightDAO.selectChildrenListByFahterId(sql);
//                Iterator var15 = childrenList.iterator();
//
//                while(var15.hasNext()) {
//                    Object childrenId = var15.next();
//                    this.tblManageRightDAO.executeSql(organization.getOrgid(),childrenId,rightid,pageid);
//                }
//            }
//        }
//    }

    @Override
    @Transient
    public void distributionPageIdByRightId(String[] rightids, String[] pageids, BigDecimal orgid) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            String sql = "";
            Integer count = 0;
            for (String rightid : rightids) {
                for (String pageid : pageids) {
                    count = this.tblManageRightDAO.selectRightCount(orgid, pageid, rightid);
                    if (count > 0) {
                        this.tblManageRightDAO.deleteBySystemBimodule(orgid, rightid, pageid);
                    }
                    this.tblManageRightDAO.insertMangeRight(orgid, pageid, rightid);
                    List<Object> childrenList = this.tblManageRightDAO.selectChildrenListByFahterId(pageid);
                    for (Object childrenId : childrenList) {
                        this.tblManageRightDAO.insertMangeRightByPageBody(orgid, childrenId, rightid, pageid);
                    }
                }
            }
        } else {
            String sql = "";
            Integer count = 0;
            for (String rightid : rightids) {
                for (String pageid : pageids) {
                    count = this.tblManageRightMySqlDAO.selectRightCount(orgid, pageid, rightid);
                    if (count > 0) {
                        this.tblManageRightMySqlDAO.deleteBySystemBimodule(orgid, rightid, pageid);
                    }
                    this.tblManageRightMySqlDAO.insertMangeRight(orgid, pageid, rightid);
                    List<Object> childrenList = this.tblManageRightMySqlDAO.selectChildrenListByFahterId(pageid);
                    for (Object childrenId : childrenList) {
                        this.tblManageRightMySqlDAO.insertMangeRightByPageBody(orgid, childrenId, rightid, pageid);
                    }
                }
            }
        }
    }

    @Override
    public Map<String, Object> findRightListbyModule(String token, String staffId) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                TblStaffUtil staff = DealUserToken.parseUserToken(token);
                BigDecimal orgid = staff.getCurrentOrg().getOrgid();
                List<TblManageRight> manageList = tblManageRightDAO.selectRightListbyModule(orgid);
                resultMap.put("code", "1");
                resultMap.put("msg", "数据访问成功");
                resultMap.put("data", manageList);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return resultMap;
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                TblStaffUtil staff = DealUserToken.parseUserToken(token);
                BigDecimal orgid = staff.getCurrentOrg().getOrgid();
                List<TblManageRightMySql> manageList = tblManageRightMySqlDAO.selectRightListbyModule(orgid);
                resultMap.put("code", "1");
                resultMap.put("msg", "数据访问成功");
                resultMap.put("data", manageList);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return resultMap;
        }
    }

    @Override
    public String getTreeListSettingMenu(BigDecimal tmplId, Map<BigDecimal, Object> map, String token, String staffId) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            List<Tree> list = new ArrayList<Tree>();
            try {
                TblStaffUtil staff = DealUserToken.parseUserToken(token);
                BigDecimal orgid = staff.getCurrentOrg().getOrgid();
                Tree tree = null;
                Integer count;
                List<TblManageRight> root = tblManageRightDAO.findBySql(tmplId, orgid);
                for (TblManageRight tblManageRight : root) {
                    tree = new Tree();
                    tree.setName(tblManageRight.getRightname());
                    tree.setId(tblManageRight.getRightid());
                    tree.setTarget("mainFramex");
                    tree.setChecked(map.get(tblManageRight.getRightid()) == null ? false : true);
                    tree.setpId(tblManageRight.getFatherrightid());
                    tree.setUrl("");
                    tree.setOpen(true);
                    count = this.tblManageRightDAO.selectChildrenList(tblManageRight.getRightid());
                    if (count > 0) {
                        tree.setIsParent(true);
                    } else {
                        tree.setIsParent(false);
                    }
                    list.add(tree);
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return com.alibaba.fastjson.JSONObject.toJSONString(list);
        } else {
            List<Tree> list = new ArrayList<Tree>();
            try {
                TblStaffUtil staff = DealUserToken.parseUserToken(token);
                BigDecimal orgid = staff.getCurrentOrg().getOrgid();
                Tree tree = null;
                Integer count;
                List<TblManageRightMySql> root = tblManageRightMySqlDAO.findBySql(tmplId, orgid);
                for (TblManageRightMySql tblManageRightMySql : root) {
                    tree = new Tree();
                    tree.setName(tblManageRightMySql.getRightname());
                    tree.setId(tblManageRightMySql.getRightid());
                    tree.setTarget("mainFramex");
                    tree.setChecked(map.get(tblManageRightMySql.getRightid()) == null ? false : true);
                    tree.setpId(tblManageRightMySql.getFatherrightid());
                    tree.setUrl("");
                    tree.setOpen(true);
                    count = this.tblManageRightMySqlDAO.selectChildrenList(tblManageRightMySql.getRightid());
                    if (count > 0) {
                        tree.setIsParent(true);
                    } else {
                        tree.setIsParent(false);
                    }
                    list.add(tree);
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return com.alibaba.fastjson.JSONObject.toJSONString(list);
        }
    }

    @Override
    public List<TblManageRight> findByManageParentId(BigDecimal fatherrightid, String token, String staffId) throws Exception {
        //List<Object[]> obs = tblManageRightDAO.OBJlistBySql(fatherrightid,orgid);
        List<TblManageRight> list = new ArrayList<TblManageRight>(0);
        TblStaffUtil staff = DealUserToken.parseUserToken(token);
        BigDecimal orgid = staff.getCurrentOrg().getOrgid();
        list = tblManageRightDAO.OBJlistBySql(fatherrightid, orgid);

//        if (list != null && list.size() > 0) {
//            Iterator var6 = list.iterator();
//
//            while(var6.hasNext()) {
//                Object[] objects = (Object[])var6.next();
//                TblManageRight trs = new TblManageRight();
//                trs.setRightid(objects[0] != null ? new BigDecimal(objects[0].toString()) : null);
//                trs.setRightname(objects[1] != null ? objects[1].toString() : objects[6].toString());
//                trs.setRighturl(objects[2] != null ? objects[2].toString() : null);
//                trs.setFatherrightid(objects[3] != null ? new BigDecimal(objects[3].toString()) : null);
//                trs.setFuncorder(objects[4] != null ? new BigDecimal(objects[4].toString()) : null);
//                trs.setIndicatorstatus(objects[5] != null ? objects[5].toString() : null);
//                list.add(trs);
//            }
//        }

        return list;
    }

    @Override
    public List<TblManageRightMySql> findByMySqlManageParentId(BigDecimal fatherrightid, String token, String staffId) throws Exception {
        List<TblManageRightMySql> list = new ArrayList<TblManageRightMySql>(0);
        TblStaffUtil staff = DealUserToken.parseUserToken(token);
        BigDecimal orgid = staff.getCurrentOrg().getOrgid();
        list = tblManageRightMySqlDAO.OBJlistBySql(fatherrightid, orgid);
        return list;
    }

    @Override
    public TblManageRight findByRightname(String rightname) {
        return tblManageRightDAO.selectWorkRightInfo(rightname);
    }

    @Override
    public TblManageRightMySql findByMySqlRightname(String rightname) {
        return tblManageRightMySqlDAO.selectWorkRightInfo(rightname);
    }

    @Override
    public String getRightForUser(String token, String staffId) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            String rightJson = null;
            Jedis jedis = null;
            try {
                TblStaffUtil staff = DealUserToken.parseUserToken(token);
                BigDecimal userId = staff.getStaffid();
                BigDecimal orgid = staff.getCurrentOrg().getOrgid();
                List<Object[]> obs = tblManageRightDAO.findListById(userId, orgid);
                List<TblManageRight> list = new ArrayList<TblManageRight>();
                if (obs != null && obs.size() > 0) {
                    for (Object[] objects : obs) {
                        TblManageRight trs = new TblManageRight();
                        trs.setRightid(objects[0] != null ? new BigDecimal(objects[0].toString()) : null);
                        trs.setRightname(objects[1] != null && objects[1] != "" ? objects[1].toString() : objects[6].toString());
                        trs.setRighturl(objects[2] != null ? objects[2].toString() : null);
                        trs.setFatherrightid(objects[3] != null ? new BigDecimal(objects[3].toString()) : null);
                        trs.setFuncorder(objects[4] != null ? new BigDecimal(objects[4].toString()) : null);
                        trs.setIndicatorstatus(objects[5] != null ? objects[5].toString() : null);
                        list.add(trs);
                    }
                }
                rightJson = JSON.toJSONString(list);

                jedis.set(RedisFinalUtis.USERMANGERRIGHT + staff.getStaffid().toString(), rightJson);
                logger.info("redis的用户key" + RedisFinalUtis.USERMANGERRIGHT + staff.getStaffid().toString() + "==value:" + jedis.get(RedisFinalUtis.USERMANGERRIGHT + staff.getStaffid().toString()));
                JedisUtil.returnResource(jedis);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return rightJson;
        } else {
            String rightJson = null;
            Jedis jedis = null;
            try {
                TblStaffUtil staff = DealUserToken.parseUserToken(token);
                BigDecimal userId = staff.getStaffid();
                BigDecimal orgid = staff.getCurrentOrg().getOrgid();
                List<Object[]> obs = tblManageRightMySqlDAO.findListById(userId, orgid);
                List<TblManageRightMySql> list = new ArrayList<TblManageRightMySql>();
                if (obs != null && obs.size() > 0) {
                    for (Object[] objects : obs) {
                        TblManageRightMySql trs = new TblManageRightMySql();
                        trs.setRightid(objects[0] != null ? new BigDecimal(objects[0].toString()) : null);
                        trs.setRightname(objects[1] != null && objects[1] != "" ? objects[1].toString() : objects[6].toString());
                        trs.setRighturl(objects[2] != null ? objects[2].toString() : null);
                        trs.setFatherrightid(objects[3] != null ? new BigDecimal(objects[3].toString()) : null);
                        trs.setFuncorder(objects[4] != null ? new BigDecimal(objects[4].toString()) : null);
                        trs.setIndicatorstatus(objects[5] != null ? objects[5].toString() : null);
                        list.add(trs);
                    }
                }
                rightJson = JSON.toJSONString(list);

                jedis.set(RedisFinalUtis.USERMANGERRIGHT + staff.getStaffid().toString(), rightJson);
                logger.info("redis的用户key" + RedisFinalUtis.USERMANGERRIGHT + staff.getStaffid().toString() + "==value:" + jedis.get(RedisFinalUtis.USERMANGERRIGHT + staff.getStaffid().toString()));
                JedisUtil.returnResource(jedis);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return rightJson;
        }
    }

    @Override
    public void delright(TblManageRight vmr) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            tblManageRightDAO.delete(vmr);
        } else {
            TblManageRightMySql vmrMySql = new TblManageRightMySql();
            BeanUtils.copyProperties(vmr,vmrMySql);
            tblManageRightMySqlDAO.delete(vmrMySql);
        }
    }

    @Override
    public void distributionPageIdByRightIdTwo(String[] rightids, String[] pageids, BigDecimal attribute, String pid) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            String sql = "";
            Integer count = 0;
            String[] var7 = rightids;
            int var8 = rightids.length;

            for (int var9 = 0; var9 < var8; ++var9) {
                String rightid = var7[var9];
                String[] var11 = pageids;
                int var12 = pageids.length;

                for (int var13 = 0; var13 < var12; ++var13) {
                    String pageid = var11[var13];
//                sql = "SELECT COUNT(0) FROM TBL_SYSTEM_BIMODULE WHERE ORGID = " + attribute.getOrgid() + " AND PAGEID = " + pid + " AND MODULERIGHTID = " + rightid;
                    count = this.tblManageRightDAO.selectRightCount(attribute, pid, rightid);
                    if (count == 0) {
//                    sql = "INSERT INTO TBL_SYSTEM_BIMODULE(ORGID, PAGEID, MODULERIGHTID) VALUES ('" + attribute.getOrgid() + "', '" + pid + "', '" + rightid + "')";
                        this.tblManageRightDAO.executeSql(attribute, pid, rightid, pageid);
                    }

//                sql = "DELETE FROM TBL_SYSTEM_BIMODULE WHERE ORGID = " + attribute.getOrgid() + " AND MODULERIGHTID = " + rightid + " AND PAGEID = " + pageid;
                    this.tblManageRightDAO.executeSql(attribute, pid, rightid, pageid);
//                sql = "INSERT INTO TBL_SYSTEM_BIMODULE(ORGID, PAGEID, MODULERIGHTID, PAGEBODY) VALUES ('" + attribute.getOrgid() + "', '" + pageid + "', '" + rightid + "'," + pid + ")";
                    this.tblManageRightDAO.executeSql(attribute, pid, rightid, pageid);
                }
            }
        } else {
            String sql = "";
            Integer count = 0;
            String[] var7 = rightids;
            int var8 = rightids.length;

            for (int var9 = 0; var9 < var8; ++var9) {
                String rightid = var7[var9];
                String[] var11 = pageids;
                int var12 = pageids.length;

                for (int var13 = 0; var13 < var12; ++var13) {
                    String pageid = var11[var13];
//                sql = "SELECT COUNT(0) FROM TBL_SYSTEM_BIMODULE WHERE ORGID = " + attribute.getOrgid() + " AND PAGEID = " + pid + " AND MODULERIGHTID = " + rightid;
                    count = this.tblManageRightMySqlDAO.selectRightCount(attribute, pid, rightid);
                    if (count == 0) {
//                    sql = "INSERT INTO TBL_SYSTEM_BIMODULE(ORGID, PAGEID, MODULERIGHTID) VALUES ('" + attribute.getOrgid() + "', '" + pid + "', '" + rightid + "')";
                        this.tblManageRightMySqlDAO.executeSql(attribute, pid, rightid, pageid);
                    }

//                sql = "DELETE FROM TBL_SYSTEM_BIMODULE WHERE ORGID = " + attribute.getOrgid() + " AND MODULERIGHTID = " + rightid + " AND PAGEID = " + pageid;
                    this.tblManageRightMySqlDAO.executeSql(attribute, pid, rightid, pageid);
//                sql = "INSERT INTO TBL_SYSTEM_BIMODULE(ORGID, PAGEID, MODULERIGHTID, PAGEBODY) VALUES ('" + attribute.getOrgid() + "', '" + pageid + "', '" + rightid + "'," + pid + ")";
                    this.tblManageRightMySqlDAO.executeSql(attribute, pid, rightid, pageid);
                }
            }
        }
    }

//    @Override
//    public String GetTree(String staffid, Map<BigDecimal, Object> map, String orgid) {
//        List<Tree> list = new ArrayList();
//        List<TblManageRight> root = this.tblManageRightDAO.getTreeRoots(staffid);
//        List<TblManageRight> list1 = this.tblManageRightDAO.getOrgRight(new BigDecimal(orgid));
//        Iterator var7 = root.iterator();
//
//        while(var7.hasNext()) {
//            TblManageRight tblManageRight = (TblManageRight)var7.next();
//            Iterator var9 = list1.iterator();
//
//            while(var9.hasNext()) {
//                TblManageRight right = (TblManageRight)var9.next();
//                if (tblManageRight.getRightid().toString().equals(right.getRightid().toString())) {
//                    Tree tree = new Tree();
//                    tree.setName(tblManageRight.getRightname());
//                    tree.setId(tblManageRight.getRightid());
//                    tree.setTarget("mainFramex");
//                    tree.setChecked(map.get(tblManageRight.getRightid()) != null);
//                    tree.setpId(tblManageRight.getFatherrightid());
//                    tree.setUrl("");
//                    tree.setOpen(true);
//                    List<Tree> childre = this.addChildren(staffid, tblManageRight.getRightid(), list1);
//                    if (childre.size() > 0) {
//                        tree.setChildren(childre);
//                    }
//
//                    list.add(tree);
//                }
//            }
//        }
//
//        return com.alibaba.fastjson.JSONObject.toJSONString(list);
//    }

    @Override
    public String GetTree(String staffid, Map<BigDecimal, Object> map, String orgid) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            List<Tree> list = new ArrayList<Tree>();
            List<TblManageRight> root = this.tblManageRightDAO.getTreeRoots(staffid);
            List<TblManageRight> list1 = this.tblManageRightDAO.getOrgRight(new BigDecimal(orgid));
            for (TblManageRight tblManageRight : root) {
                for (TblManageRight right : list1) {
                    if (tblManageRight.getRightid().toString().equals(right.getRightid().toString())) {
                        Tree tree = new Tree();
                        tree.setName(tblManageRight.getRightname());
                        tree.setId(tblManageRight.getRightid());
                        tree.setTarget("mainFramex");
                        tree.setChecked(map.get(tblManageRight.getRightid()) == null ? false : true);
                        tree.setpId(tblManageRight.getFatherrightid());
                        tree.setUrl("");
                        tree.setOpen(true);
                        List<Tree> childre = this.addChildren1(staffid, tblManageRight.getRightid(), map, list1);
                        if (childre.size() > 0) {
                            tree.setChildren(childre);
                        }
                        list.add(tree);
                    }
                }
            }
            return JSONObject.toJSONString(list);
        } else {
            List<Tree> list = new ArrayList<Tree>();
            List<TblManageRightMySql> root = this.tblManageRightMySqlDAO.getTreeRoots(staffid);
            List<TblManageRightMySql> list1 = this.tblManageRightMySqlDAO.getOrgRight(new BigDecimal(orgid));
            for (TblManageRightMySql tblManageRightMySql : root) {
                for (TblManageRightMySql right : list1) {
                    if (tblManageRightMySql.getRightid().toString().equals(right.getRightid().toString())) {
                        Tree tree = new Tree();
                        tree.setName(tblManageRightMySql.getRightname());
                        tree.setId(tblManageRightMySql.getRightid());
                        tree.setTarget("mainFramex");
                        tree.setChecked(map.get(tblManageRightMySql.getRightid()) == null ? false : true);
                        tree.setpId(tblManageRightMySql.getFatherrightid());
                        tree.setUrl("");
                        tree.setOpen(true);
                        List<Tree> childre = this.addMySqlChildren1(staffid, tblManageRightMySql.getRightid(), map, list1);
                        if (childre.size() > 0) {
                            tree.setChildren(childre);
                        }
                        list.add(tree);
                    }
                }
            }
            return JSONObject.toJSONString(list);
        }
    }

    public List<Tree> addChildren1(String staffid, BigDecimal pId, Map<BigDecimal, Object> map, List<TblManageRight> lists) {
        List<Tree> list = new ArrayList<Tree>();
        List<TblManageRight> root = this.tblManageRightDAO.getTreeByNodeId(pId);
        for (TblManageRight tblManageRight : root) {
            for (TblManageRight right : lists) {
                if (tblManageRight.getRightid().toString().equals(right.getRightid().toString())) {
                    Tree tree = new Tree();
                    tree.setName(tblManageRight.getRightname());
                    tree.setId(tblManageRight.getRightid());
                    tree.setTarget("mainFramex");
                    tree.setChecked(map.get(tblManageRight.getRightid()) == null ? false : true);
                    tree.setpId(tblManageRight.getFatherrightid());
                    tree.setUrl("");
                    tree.setIsParent(false);
                    tree.setUrl("");
                    List<Tree> trees = addChildren1(staffid, tblManageRight.getRightid(), map, lists);
                    if (trees.size() > 0) {
                        tree.setChildren(trees);
                    }
                    list.add(tree);
                }
            }
        }
        return list;
    }

    public List<Tree> addMySqlChildren1(String staffid, BigDecimal pId, Map<BigDecimal, Object> map, List<TblManageRightMySql> lists) {
        List<Tree> list = new ArrayList<Tree>();
        List<TblManageRightMySql> root = this.tblManageRightMySqlDAO.getTreeByNodeId(pId);
        for (TblManageRightMySql tblManageRight : root) {
            for (TblManageRightMySql right : lists) {
                if (tblManageRight.getRightid().toString().equals(right.getRightid().toString())) {
                    Tree tree = new Tree();
                    tree.setName(tblManageRight.getRightname());
                    tree.setId(tblManageRight.getRightid());
                    tree.setTarget("mainFramex");
                    tree.setChecked(map.get(tblManageRight.getRightid()) == null ? false : true);
                    tree.setpId(tblManageRight.getFatherrightid());
                    tree.setUrl("");
                    tree.setIsParent(false);
                    tree.setUrl("");
                    List<Tree> trees = addMySqlChildren1(staffid, tblManageRight.getRightid(), map, lists);
                    if (trees.size() > 0) {
                        tree.setChildren(trees);
                    }
                    list.add(tree);
                }
            }
        }
        return list;
    }


    public List<Tree> addChildren(Serializable tmplId, BigDecimal pId, List<TblManageRight> lists) {
        List<Tree> list = new ArrayList();
        List<TblManageRight> root = this.tblManageRightDAO.getTreeByNodeId(pId);
        Iterator var6 = root.iterator();

        while (var6.hasNext()) {
            TblManageRight tblManageRight = (TblManageRight) var6.next();
            Iterator var8 = lists.iterator();

            while (var8.hasNext()) {
                TblManageRight right = (TblManageRight) var8.next();
                if (tblManageRight.getRightid().toString().equals(right.getRightid().toString())) {
                    Tree tree = new Tree();
                    tree.setName(tblManageRight.getRightname());
                    tree.setId(tblManageRight.getRightid());
                    tree.setTarget("mainFramex");
                    tree.setChecked(tblManageRight.getRightid() != null);
                    tree.setpId(tblManageRight.getFatherrightid());
                    tree.setUrl("");
                    tree.setIsParent(false);
                    tree.setUrl("");
                    List<Tree> trees = this.addChildren(tmplId, tblManageRight.getRightid(), lists);
                    if (trees.size() > 0) {
                        tree.setChildren(trees);
                    }

                    list.add(tree);
                }
            }
        }

        return list;
    }

    public List<Tree> addMySqlChildren(Serializable tmplId, BigDecimal pId, List<TblManageRightMySql> lists) {
        List<Tree> list = new ArrayList();
        List<TblManageRightMySql> root = this.tblManageRightMySqlDAO.getTreeByNodeId(pId);
        Iterator var6 = root.iterator();

        while (var6.hasNext()) {
            TblManageRightMySql tblManageRightMySql = (TblManageRightMySql) var6.next();
            Iterator var8 = lists.iterator();

            while (var8.hasNext()) {
                TblManageRightMySql right = (TblManageRightMySql) var8.next();
                if (tblManageRightMySql.getRightid().toString().equals(right.getRightid().toString())) {
                    Tree tree = new Tree();
                    tree.setName(tblManageRightMySql.getRightname());
                    tree.setId(tblManageRightMySql.getRightid());
                    tree.setTarget("mainFramex");
                    tree.setChecked(tblManageRightMySql.getRightid() != null);
                    tree.setpId(tblManageRightMySql.getFatherrightid());
                    tree.setUrl("");
                    tree.setIsParent(false);
                    tree.setUrl("");
                    List<Tree> trees = this.addMySqlChildren(tmplId, tblManageRightMySql.getRightid(), lists);
                    if (trees.size() > 0) {
                        tree.setChildren(trees);
                    }

                    list.add(tree);
                }
            }
        }

        return list;
    }

    @Override
    public JsonBean findRightLIstByUser(String token, Integer rightId) throws Exception {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            if (rightId == null) {
                rightId = -1;
            }

            List<TblManageRight> rightList = this.tblManageRightDAO.selectChildrenRightListByUser(rightId, loginStaff.getStaffid(), loginStaff.getLinkOrg().getOrgid());
            this.setChidrenRightList(rightList, loginStaff.getStaffid(), loginStaff.getLinkOrg().getOrgid());
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("rightList", rightList);
            return ResponseFormat.retParam(1, 200, resultMap);
        } else {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            if (rightId == null) {
                rightId = -1;
            }

            List<TblManageRightMySql> rightList = this.tblManageRightMySqlDAO.selectChildrenRightListByUser(rightId, loginStaff.getStaffid(), loginStaff.getLinkOrg().getOrgid());
            this.setMySqlChidrenRightList(rightList, loginStaff.getStaffid(), loginStaff.getLinkOrg().getOrgid());
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("rightList", rightList);
            return ResponseFormat.retParam(1, 200, resultMap);
        }
    }

    private void setChidrenRightList(List<TblManageRight> rightList, BigDecimal staffid, BigDecimal orgid) throws Exception {
        List<TblManageRight> childrenList = null;
        for (TblManageRight right : rightList) {
            childrenList = this.tblManageRightDAO.selectChildrenRightListByUser(right.getRightid().intValue(), staffid, orgid);
            if (childrenList == null || childrenList.size() == 0) {
                continue;
            }
            this.setChidrenRightList(childrenList, staffid, orgid);
            right.setRightList(childrenList);
        }
    }

    private void setMySqlChidrenRightList(List<TblManageRightMySql> rightList, BigDecimal staffid, BigDecimal orgid) throws Exception {
        List<TblManageRightMySql> childrenList = null;
        for (TblManageRightMySql right : rightList) {
            childrenList = this.tblManageRightMySqlDAO.selectChildrenRightListByUser(right.getRightid().intValue(), staffid, orgid);
            if (childrenList == null || childrenList.size() == 0) {
                continue;
            }
            this.setMySqlChidrenRightList(childrenList, staffid, orgid);
            right.setRightList(childrenList);
        }
    }

    @Override
    public JsonBean findChildrenRightListByUser(String token, Integer rightId)
            throws Exception {
        TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
        if (loginStaff == null) {
            return ResponseFormat.retParam(0, 20006, null);
        }
        if (rightId == null) {
            rightId = -1;
        }
        List<TblManageRight> rightList = this.tblManageRightDAO.selectChildrenRightListByUser(rightId, loginStaff.getStaffid(), loginStaff.getLinkOrg().getOrgid());
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        resultMap.put("rightList", rightList);
        return ResponseFormat.retParam(1, 200, resultMap);
    }

    @Override
    public JsonBean saveManageRight(TblManageRight right, String token) throws Exception {
        TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
        if (loginStaff == null) {
            return ResponseFormat.retParam(0, 20006, null);
        }
        this.tblManageRightDAO.insertTblManageRight(right);
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        resultMap.put("rightId", right.getRightid());
        return ResponseFormat.retParam(1, 200, resultMap);
    }

    @Override
    public JsonBean saveMySqlManageRight(TblManageRightMySql right, String token) throws Exception {
        TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
        if (loginStaff == null) {
            return ResponseFormat.retParam(0, 20006, null);
        }
        this.tblManageRightMySqlDAO.insertTblManageRight(right);
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        resultMap.put("rightId", right.getRightid());
        return ResponseFormat.retParam(1, 200, resultMap);
    }

    @Override
    public JsonBean findRightEntityById(BigDecimal rightId) throws Exception {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            TblManageRight right = this.tblManageRightDAO.findById(rightId);
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("right", right);
            return ResponseFormat.retParam(1, 200, resultMap);
        } else {
            TblManageRightMySql right = this.tblManageRightMySqlDAO.findById(rightId);
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("right", right);
            return ResponseFormat.retParam(1, 200, resultMap);
        }
    }

    @Override
    public JsonBean modifyManageRight(TblManageRight right, String token) throws Exception {
        TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
        if (loginStaff == null) {
            return ResponseFormat.retParam(0, 20006, null);
        }
        this.tblManageRightDAO.updateTblManageRight(right);
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        resultMap.put("rightId", right.getRightid());
        return ResponseFormat.retParam(1, 200, resultMap);
    }

    @Override
    public JsonBean modifyMySqlManageRight(TblManageRightMySql right, String token) throws Exception {
        TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
        if (loginStaff == null) {
            return ResponseFormat.retParam(0, 20006, null);
        }
        this.tblManageRightMySqlDAO.updateTblManageRight(right);
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        resultMap.put("rightId", right.getRightid());
        return ResponseFormat.retParam(1, 200, resultMap);
    }

    @Override
    @Transactional(value = "oracleDataSourceTransactionManager", rollbackFor = Exception.class, timeout = 36000)
    public JsonBean removeManageRight(BigDecimal rightId) throws Exception {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            String sql = "DELETE FROM TBL_MANAGE_USER_RIGHT WHERE RIGHTID = " + rightId;
            this.tblManageRightDAO.executeDeleteSql(sql);
            sql = "DELETE FROM TBL_ORG_RIGHT WHERE RIGHTID = " + rightId;
            this.tblManageRightDAO.executeDeleteSql(sql);
            sql = "DELETE FROM TBL_ORG_RIGHT_NEW WHERE RIGHTID = " + rightId;
            this.tblManageRightDAO.executeDeleteSql(sql);
            sql = "DELETE FROM TBL_MANAGE_RIGHT WHERE RIGHTID = " + rightId;
            this.tblManageRightDAO.executeDeleteSql(sql);
            return ResponseFormat.retParam(1, 200, null);
        } else {
            String sql = "DELETE FROM TBL_MANAGE_USER_RIGHT WHERE RIGHTID = " + rightId;
            this.tblManageRightMySqlDAO.executeDeleteSql(sql);
            sql = "DELETE FROM TBL_ORG_RIGHT WHERE RIGHTID = " + rightId;
            this.tblManageRightMySqlDAO.executeDeleteSql(sql);
            sql = "DELETE FROM TBL_ORG_RIGHT_NEW WHERE RIGHTID = " + rightId;
            this.tblManageRightMySqlDAO.executeDeleteSql(sql);
            sql = "DELETE FROM TBL_MANAGE_RIGHT WHERE RIGHTID = " + rightId;
            this.tblManageRightMySqlDAO.executeDeleteSql(sql);
            return ResponseFormat.retParam(1, 200, null);
        }
    }

    // tblManageScreenRightDAO.deleteUserId(userid);
//    String a[] = priid.split(",");
//        for (String pri : a){
//        tblManageScreenRightDAO.insertUserId(userid,pri);
//    }
    @Override
    public void grantScreenRight(String userid, String priid) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            tblManageRightDAO.deleteUserId(userid);
            String[] rightIds = priid.split(",");
            for (String rightId : rightIds) {
                tblManageRightDAO.insertUserId(userid, rightId);
            }
        } else {
            tblManageRightMySqlDAO.deleteUserId(userid);
            String[] rightIds = priid.split(",");
            for (String rightId : rightIds) {
                tblManageRightMySqlDAO.insertUserId(userid, rightId);
            }
        }
    }

    @Override
    public void save(TblManageRight viewTblManageRight) {
        this.tblManageRightDAO.insertSelective(viewTblManageRight);
    }

    @Override
    public void saveMySql(TblManageRightMySql viewTblManageRightMySql) {
        this.tblManageRightMySqlDAO.insertSelective(viewTblManageRightMySql);
    }

    @Override
    public List<TblManageRight> findByUserAll(String userid) {
        return tblManageRightDAO.findByUserAll(userid);
    }

    @Override
    public List<TblManageRightMySql> findByMySqlUserAll(String userid) {
        return tblManageRightMySqlDAO.findByUserAll(userid);
    }

    @Override
    public void updateright(TblManageRight tblManageRight) {
        this.tblManageRightDAO.updateByPrimaryKeySelective(tblManageRight);
    }

    @Override
    public void updateMySqlright(TblManageRightMySql tblManageRightMySql) {
        this.tblManageRightMySqlDAO.updateByPrimaryKeySelective(tblManageRightMySql);
    }

    @Override
    public void inserUserRight(TblManageUserRight userRight) {
        this.tblManageUserRightMapper.inserUserRight(userRight);
    }

    @Override
    public void inserMySqlUserRight(TblManageUserRightMySql userRightMySql) {
        this.tblManageUserRightMySqlMapper.inserUserRight(userRightMySql);
    }

    @Override
    public List<TblManageRight> findByorgid(BigDecimal id) {
        return this.tblManageRightDAO.findByorgid(id);
    }

    @Override
    public List<TblManageRightMySql> findByMySqlorgid(BigDecimal id) {
        return this.tblManageRightMySqlDAO.findByorgid(id);
    }

    @Override
    public List<TblBiReportMenu> findBiReportList(BigDecimal orgid, BigDecimal rightid) throws Exception {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            // TODO Auto-generated method stub
            return null;
        } else {
            // TODO Auto-generated method stub
            return null;
        }
    }
}
